package com.ruoyi.web.controller.api;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.web.domian.sqlReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.UUID;

@RestController
public class upfrieSql extends BaseController {
    @Autowired
    private ServerConfig serverConfig;
        @Value("${upload.path:/ruoyi/uploadPath/upload/wx}") // 应用属性中配置的上传目录
        private String uploadPath;

        @PostMapping("/api/upload")
        public sqlReturn handleFileUpload(@RequestParam("image") MultipartFile file) {
            if (file.isEmpty()) {
                return new sqlReturn(null,"上传文件失败",400);
            }

            try {
                // 生成唯一的文件名，避免重名覆盖
                String fileName = UUID.randomUUID().toString() + "_" + StringUtils.cleanPath(file.getOriginalFilename());
                Path uploadPathResolved = Paths.get(uploadPath).resolve(fileName);

                // 创建文件夹如果不存在
                Files.createDirectories(uploadPathResolved.getParent());

                // 保存文件到服务器
                Files.copy(file.getInputStream(), uploadPathResolved);

                //返回  文件路径  完整的http 路径 便于前端
                String httpFileUrl = serverConfig.getUrl() +"/profile";
                String fileUrl = String.valueOf(uploadPathResolved.toAbsolutePath());
                HashMap<String, Object> requestParams = new HashMap<>();
                requestParams.put("http",httpFileUrl);
                requestParams.put("path",fileUrl);

                return new sqlReturn(requestParams,"上传文件成功",200);


            } catch (IOException e) {
                e.printStackTrace();
                return new sqlReturn(null,"上传文件失败",500);

            }
        }


    @PostMapping("/api/uploadfrie")
    @ResponseBody
    public sqlReturn uploadfrie(@RequestParam("all") MultipartFile file,String Name) {
        System.out.println(Name+"\t");
        System.out.println(file+"\t");
        if (file.isEmpty()) {
            return new sqlReturn(null,"上传文件失败",400);
        }

        try {
            // 生成唯一的文件名，避免重名覆盖
            String fileName = Name+"_"+UUID.randomUUID().toString()  + StringUtils.cleanPath(file.getOriginalFilename());
            Path uploadPathResolved = Paths.get(uploadPath).resolve(fileName);

            // 创建文件夹如果不存在
            Files.createDirectories(uploadPathResolved.getParent());

            // 保存文件到服务器
            Files.copy(file.getInputStream(), uploadPathResolved);

            //返回  文件路径  完整的http 路径 便于前端
            String httpFileUrl = serverConfig.getUrl() +"/profile";
            String fileUrl = String.valueOf(uploadPathResolved.toAbsolutePath());
            HashMap<String, Object> requestParams = new HashMap<>();
            requestParams.put("http",httpFileUrl);
            requestParams.put("path",fileUrl);

            return new sqlReturn(requestParams,"上传文件成功",200);


        } catch (IOException e) {
            e.printStackTrace();
            return new sqlReturn(null,"上传文件失败",500);

        }
    }
}
